<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/23
 * Time: 15:26
 */

namespace app\adminapiv3\service\asset_manage\software;


use app\adminapiv3\service\BaseService;
use app\common\service\asset_manage\software\HostSoftware;

class SoftwareService extends BaseService
{

    /**
     * @name 列表
     * @param $params
     * @return array
     * @author wx
     * @date 2019/10/23 15:30
     */
    public function list($params)
    {
        $server = new HostSoftware();
        $data = $server->list($params);

        return $data;
    }

    /**
     * @name 添加
     * @param $addData
     * @author wx
     * @date 2019/10/23 15:39
     */
    public function add($addData)
    {
        global $logArray;
        $logArray['content'] = '添加业务系统';

        //1. 验证重复
        if ($this->checkExist(['name' => $addData['name']])) {
            $this->setError($this->CODE_FAIL,'请勿添加重复的业务系统名称');
            return false;
        }

        $onTime = $addData['on_time'];

        //2. 添加
        $addData['status'] = 1;
        $addData['off_time'] = 0;
        $addData['ctime'] = time();
        $addData['on_time'] = strtotime($addData['on_time']);

        $server = new HostSoftware();
        $server->add($addData);

        $logArray['new_info'] = '业务系统名称：'.$addData['name'].'；功能说明：'.$addData['description'].'；上线时间：'.$onTime;

    }

    /**
     * @name 检查重复
     * @param $condition
     * @return bool
     * @author wx
     * @date time
     */
    public function checkExist($condition)
    {
        $server = new HostSoftware();
        $info = $server->findData($condition);

        if(!$info){
            return false;
        }

        return true;
    }



    /**
     * @name 修改
     * @param $id
     * @param $updateData
     * @author wx
     * @date 2019/10/23 15:39
     */
    public function modify($id, $updateData)
    {
        global $logArray;
        $logArray['content'] = '修改业务系统';

        $server = new HostSoftware();

        if($updateData['status'] == 2){//2已退役
            $updateData['off_time'] = time();
        }else{
            $updateData['off_time'] = 0;
        }

        $info = $server->findData(['id' => $id]);
        if(!$info){
            $this->setError($this->CODE_FAIL, '该业务系统不存在');
            return false;
        }

        $onTime = $updateData['on_time'];
        $updateData['on_time'] = strtotime($updateData['on_time']);

        $server->modify(['id' => $id], $updateData);

        $logArray['old_info'] = '业务系统名称：'.$info['name'].'；功能说明：'.$info['description'].'；上线时间：'.date('Y-m-d',$info['on_time']);
        $logArray['new_info'] = '业务系统名称：'.$updateData['name'].'；功能说明：'.$updateData['description'].'；上线时间：'.$onTime;
    }

    /**
     * @name 删除
     * @param $id
     * @author wx
     * @date 2019/10/23 15:40
     */
    public function del($id)
    {
        global $logArray;
        $logArray['content'] = '修改业务系统';

        $condition[] = array('id', 'in', $id);
        $server = new HostSoftware();

        $info = $server->findData(['id' => $id]);
        if(!$info){
            $this->setError($this->CODE_FAIL, '该业务系统不存在');
            return false;
        }
        $server->del($condition);

        $logArray['old_info'] = '业务系统名称：'.$info['name'].'；功能说明：'.$info['description'].'；上线时间：'.date('Y-m-d',$info['on_time']);
    }

    /**
     * @name 退役
     * @param $id
     * @author wx
     * @date 2020/10/22 13:38
     */
    public function retire($id)
    {
        global $logArray;
        $logArray['content'] = '退役业务系统';

        $updataData['status'] = 2;
        $updataData['off_time'] = time();

        $server = new HostSoftware();
        $server->modify(['id' => $id], $updataData);
    }
}